import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetFile {
    public static void  downLoadFromUrl(String fileName,String savePath) throws IOException{
        File file=new File(savePath);
        //判断文件夹是否存在
        if (!file.exists())
        {
            //如果文件夹不存在，则创建新的的文件夹
            file.mkdirs();
        }
        String url = "https://quotsoft.net/air/data/";
        FileOutputStream fileOut = null;
        HttpURLConnection conn = null;
        InputStream inputStream = null;
        try
        {
            // 建立链接
            StringBuffer tmp = new StringBuffer(url);
            tmp.append(fileName);
            url = tmp.toString();
            URL httpUrl=new URL(url);
            conn=(HttpURLConnection) httpUrl.openConnection();
            //以Post方式提交表单，默认get方式
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // post方式不能使用缓存
            conn.setUseCaches(false);
            //连接指定的资源
            conn.connect();
            //获取网络输入流
            inputStream=conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            //判断文件的保存路径后面是否以/结尾
            if (!savePath.endsWith("/")) {
                savePath += "/";
            }
            //写入到文件（注意文件保存路径的后面一定要加上文件的名称）
            fileOut = new FileOutputStream(savePath+fileName);
            BufferedOutputStream bos = new BufferedOutputStream(fileOut);

            byte[] buf = new byte[4096];
            int length = bis.read(buf);
            //保存文件
            while(length != -1)
            {
                bos.write(buf, 0, length);
                length = bis.read(buf);
            }
            bos.close();
            bis.close();
            conn.disconnect();
        } catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("抛出异常！！");
        }
    }

    public static void main(String[] args) throws IOException {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        String[] tmp = formatter.format(date).split(" ");
        String[] tmp1 = tmp[0].split("-");
        int month = Integer.parseInt(tmp1[1]);
        int day = Integer.parseInt(tmp1[2]);
        if(month > 1){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"010"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 31;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"01"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 2){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"020"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 28;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"02"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 3){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"030"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 31;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"03"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 4){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"040"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 30;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"04"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 5){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"050"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 31;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"05"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 6){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"060"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 30;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"06"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 7){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"070"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 31;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"07"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 8){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"080"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 31;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"08"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 9){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"090"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 30;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"09"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 10){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"100"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 31;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"10"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(month > 11){
            for(int i = 1;i <= 9;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"110"+i+".csv",System.getProperty("user.dir"));
            }
            for(int i = 10;i <= 30;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+"11"+i+".csv",System.getProperty("user.dir"));
            }
        }
        if(day > 9){
            for(int i = 10;i <= day;i++){
                downLoadFromUrl("china_cites_"+tmp1[0]+tmp[1]+i+".csv",System.getProperty("user.dir"));
            }
        }
        for(int i = 1;i<=9&&i<=day;i++){
            downLoadFromUrl("china_cites_"+tmp1[0]+tmp[1]+"0"+i+".csv",System.getProperty("user.dir"));
        }
    }
}
